package com.twang.stock.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.twang.stock.common.http.HtmlTools;
import com.twang.stock.dto.EbayRequest;
import com.twang.stock.dto.TenXunStock;
import com.twang.stock.ebay.EbaySearchByTypeService;
import com.twang.stock.ebay.EbayService;
import com.twang.stock.entity.CompanyStockMinutes;
import com.twang.stock.entity.LongHuInfo;
import com.twang.stock.feign.ZuoTiStatusFeignService;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.rocketMq.RocketMQProducer;
import com.twang.stock.service.*;
import com.twang.stock.service.jinyan.GetStockListService;
import com.twang.stock.service.jinyan.UpdateAndFixDataService;
import com.twang.stock.service.jinyan.UpdtePriceAndVolService;

import com.twang.stock.strategy.Stock60DayMaxVol;
import com.twang.stock.strategy.WarnVipNewsService;
import com.twang.stock.task.*;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import com.twang.stock.utils.R;
import com.twang.stock.utils.date.HolidayCopy;
import com.twang.stock.utils.http.HttpStockUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/utils")
@Slf4j
public class UtilController {


    @Autowired
    private WarnVipNewsService warnVipNewsService;

    @Autowired
    private WeiPanJianQianService weiPanJianQianService;

    @Autowired
    private KaiPanLaZiJinTask kaiPanLaZiJinTask;

    @Autowired
    private GetStockListService getStockListService;

    @Autowired
    private UpdateAndFixDataService updateAndFixDataService;

    @Autowired
    private UpdtePriceAndVolService updtePriceAndVolService;

    @Autowired
    private StockMinutesTask stockMinutesTask;

    @Autowired
    private QueryZhangDieTingTask queryZhangDieTingTask;


    @Autowired
    private CompanyStockMinutesService companyStockMinutesService;

    @Autowired
    private LongHuBangService longHuBangService;

    @Autowired
    private LongHUDataTask longHUDataTask;


    @Autowired
    private EbayService ebayService;


    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    @Resource
    private TodayTrade50AmountTask todayTrade50AmountTask;

    @Autowired
    private AsycTaskExecutorConfig asycTaskExecutorConfig;

    @Autowired
    private ZhangTingJuJiaoJobTask zhangTingJuJiaoJobTask;

    @Autowired
    private TodayReDuOnehourTask todayReDuOnehourTask;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private FuPanTask fuPanTask;

    @Autowired
    private StockLastChengJiaoETask stockLastChengJiaoETask;

    @Autowired
    private StockLastMinutesETask stockLastMinutesETask;
    @Autowired
    private NotifyStockBuyTask notifyStockBuyTask;

    @Autowired
    private MinutesStockFixTask minutesStockFixTask;

    @Autowired
    private RocketMQProducer rocketMQProducer;

    @Autowired
    private  DaPanYiDongTask daPanYiDongTask;

    @Autowired
    private StockMinutesTradeMoreWarnTask stockMinutesTradeMoreWarnTask;


    @Autowired
    private Stock60DayMaxVol stock60DayMaxVol;


    @Autowired
    private ZhangTingBanTaskService zhangTingBanTaskService;

    @Autowired
    private ZuoTiStatusFeignService zuoTiStatusFeignService;
    @Autowired
    private ZhangTingjuJiaoService zhangTingjuJiaoService;

    @Autowired
    private EbaySearchByTypeService ebaySearchByTypeService;

    @Autowired
    private WanZhuBeiChiChangService wanZhuBeiChiChangService;
    @RequestMapping(value = "/longHUDataTask")
    public String longHUDataTask() throws Exception {
        longHUDataTask.exceuteToDayLongHuData();
        return  "大盘温度预警";
    }

    @RequestMapping(value = "/wanZhuBeiChiChang")
    public String  wanZhuBeiChiChang() throws Exception {
        wanZhuBeiChiChangService.根据日期更新玩主杯热榜(HolidayCopy.getToday());
        return  "根据日期更新玩主杯热榜";
    }


    @RequestMapping(value = "/daPanYiDongTask")
    public String daPanYiDongTask(){
        daPanYiDongTask.大盘温度预警();
        return  "大盘温度预警";
    }

    @RequestMapping(value = "/sendMailWithByZuoT")
    public String sendMailWithByZuoT(){
        String msg=  new StringBuffer("messages").append("-").append("p.getStockName()").append("-").append("p.getStockCode()").append("-").append("wangtao_27520@qq.com").toString();
         rocketMQProducer.sendOneWay(msg);
        return  msg;
    }

    @RequestMapping(value = "/extractedChengJiaoe")
    public String extractedChengJiaoe(){
        stockMinutesTask.extractedChengJiaoe();
        return  "成功";
    }




    @RequestMapping(value = "/test")
    public ModelAndView index(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("tags_longhubang");
        return  modelAndView;
    }

    /**
     *
     */

    @RequestMapping(value = "/notifyStockBuyTask")
    public String notifyStockBuyTask() throws Exception {
        notifyStockBuyTask.刷新数据();
        return  "执行完成";
    }

    @RequestMapping(value = "/notifyZuoT")
    public String notifyZuoT() throws Exception {
        notifyStockBuyTask.做T预警();
//        StockMinutesMessage message = new StockMinutesMessage();
//        message.setMinutes(HolidayCopy.getHHmmss().substring(8, 12));
//        message.setDate(HolidayCopy.getRealToday());
//        message.setType(StockMinutesMessage.TYPE_MINUTES_ZUO_T);
//        log.info(">>>>>>>>>>>> covertDTO  message={}",JSONObject.toJSONString(message));
//        rocketMQProducer.sendOneWay(JSONObject.toJSONString(message));
        return  "执行完成";
    }

    @RequestMapping(value = "/minutesStockFixTask")
    public String minutesStockFixTask(String date) throws Exception {
        minutesStockFixTask.initMinutesDataNew();
        return  "执行完成";
    }

    @RequestMapping(value = "/NDZFB")
    public String 年度涨幅榜更新(String date) throws Exception {
        minutesStockFixTask.年度涨幅榜更新();
        return  "执行完成";
    }

    @RequestMapping(value = "/getZDT")
    public String getZDT(String code,String name,String date) throws Exception {
        minutesStockFixTask.getZDT(code, name,date);
        return "执行完成";
    }



    /**
     *
     */

    @RequestMapping(value = "/stockLastMinutesETask")
    public String stockLastMinutesETask() throws InterruptedException {
        stockLastMinutesETask.excute();
        return  "执行完成";
    }

    @RequestMapping(value = "/stockLastChengJiaoETask")
    public void stockLastChengJiaoETask() {
        log.info("UtilController ---> fuPanTask start");
        stockLastChengJiaoETask.excute();
    }

    @RequestMapping(value = "/fuPanTask")
    public void fuPanTask() throws MessagingException {
        log.info("UtilController ---> fuPanTask start");
        fuPanTask.excute();
    }

    @RequestMapping(value = "/delKey")
    public void delKey(String key){
        redisUtil.delKeys(key);
    }

    @RequestMapping(value = "/getKey")
    public String getKey(String key) {
        return redisUtil.get(key).toString();
    }


    /**
     * 初始化所有股票名称
     * @return
     */
    @RequestMapping(value = "/getStockName")
    public String getStockName() {
        List list = Lists.newArrayList();
        try {
            getStockListService.getStockName();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    /**
     * 修正历史数据
     * @return
     * url：http://127.0.0.1:8080/stock/updateAndFixData?year=2022&season=1
     */
    @RequestMapping(value = "/updateAndFixData")
    public String updateAndFixData() {
        List list = Lists.newArrayList();
        try {
            updateAndFixDataService.excute("2024", "4");
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     *  更新5日、10日、20日的价格和成交量
     * @return
     */
    @RequestMapping(value = "/updtePriceAndVolService")
    @ResponseBody
    public String updtePriceAndVolService() {
        List list = Lists.newArrayList();
        try {
            updtePriceAndVolService.updtePriceAndVol();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    /**
     *  http://127.0.0.1:8081/utils/stockMinutesTask
     * @return
     */
    @RequestMapping(value = "/stockMinutesTask")
    @ResponseBody
    public String stockMinutesTask() {
        List list = Lists.newArrayList();
        try {
            //stockMinutesTask.initMinutesData();
            stockMinutesTask.涨停板连扳数据();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    /**
     *  http://127.0.0.1:8081/utils/chengJiaoECache
     * @return
     */
    @RequestMapping(value = "/chengJiaoECache")
    @ResponseBody
    public String chengJiaoECache() {
        List list = Lists.newArrayList();
        try {
            zhangTingBanTaskService.chengJiaoECache();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }





    /**
     *  http://127.0.0.1:8081/utils/queryZhangDieTingTask
     * @return
     */
    @RequestMapping(value = "/queryZhangDieTingTask")
    @ResponseBody
    public String queryZhangDieTingTask() {
        List list = Lists.newArrayList();
        try {
            queryZhangDieTingTask.getZDT();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    /**
     *  http://127.0.0.1:8081/utils/queryDatesFixHistoryData
     * @return
     */
    @RequestMapping(value = "/queryDatesFixHistoryData")
    @ResponseBody
    public String queryDatesFixHistoryData() {
        List list = Lists.newArrayList();
        try {
            stockMinutesTask.queryDatesFixHistoryData();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    /**
     *  http://127.0.0.1:8081/utils/getLongHuBangInfo?date=20220322
     * @return
     */
    @RequestMapping(value = "/getLongHuBangInfo")
    @ResponseBody
    public String queryDatesFixHistoryData( LongHuInfo param) {
        List list = Lists.newArrayList();
        try {
         List<LongHuInfo> result=  longHuBangService.query(param);
            result.forEach(item->{
                try {
                    longHUDataTask.getLongHuBangInfo(item);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     *  http://127.0.0.1:8081/utils/fixTodayMinutes
     * @return
     */
    @RequestMapping(value = "/fixTodayMinutes")
    @ResponseBody
    public String fixTodayMinutes() {
        List list = Lists.newArrayList();
        try {
            companyStockMinutesService.fixTodayMinutes();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }
    /**
     *  http://127.0.0.1:8081/utils/zhangtingReason
     * @return
     */
    @RequestMapping(value = "/zhangtingReason")
    @ResponseBody
    public String zhangtingReason() {
        List list = Lists.newArrayList();
        try {
            HttpStockUtils httpStockUtils=new HttpStockUtils();
            Map<String, String> params = new HashMap<>();
            params.put("PhoneOSNew", "2");
            params.put("StockID", "600649");
            params.put("VerSion", "5.4.0.0");
            params.put("apiv", "w29");
            params.put("a", "GetKLineZhangTing");
            params.put("c", "StockLineData");
            String str = httpStockUtils.postForm("https://apphq.longhuvip.com/w1/api/index.php", params);
            System.out.println(str);
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    /**
     *  http://127.0.0.1:8081/utils/geZDAmount
     * @return
     */
    @RequestMapping(value = "/geZDAmount")
    @ResponseBody
    public String geZDAmount()  {
        //查询当日成交额前50
        Example example = new Example(CompanyStockMinutes.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("date","20220805");
        criteria.andEqualTo("companyCode","sz002466");
        example.orderBy("id").asc();
        List<CompanyStockMinutes> list = companyStockMinutesMapper.selectByExample(example);
        list.forEach(item->{
            asycTaskExecutorConfig.taskQueueExecutor().execute(() -> {
                try {
                    todayTrade50AmountTask.excuteQuery(item.getMinutes(),item.getDate());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        });
        return "SUCCESS";
    }


    /**
     *  http://127.0.0.1:8081/utils/todayReDuOnehourTask
     * @return
     */
    @RequestMapping(value = "/todayReDuOnehourTask")
    @ResponseBody
    public String todayReDuOnehourTask() {
        List list = Lists.newArrayList();
        try {
            todayReDuOnehourTask.热点预警();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }
    /**
     *  http://127.0.0.1:8081/utils/todayReDuOnehourTask
     * @return
     */
    @RequestMapping(value = "/zhangTingJuJiaoTask")
    @ResponseBody
    public String zhangTingJuJiaoTask() {
        List list = Lists.newArrayList();
        try {
            zhangTingJuJiaoJobTask.extracted();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    @RequestMapping(value = "/stockMinutesTradeMoreWarnTask")
    @ResponseBody
    public String stockMinutesTradeMoreWarnTask() {
        List list = Lists.newArrayList();
        try {
            stockMinutesTradeMoreWarnTask.initMinutesData();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    @RequestMapping(value = "/stock60DayMaxVol")
    @ResponseBody
    public String stock60DayMaxVol() {
        List list = Lists.newArrayList();
        try {
            stock60DayMaxVol.initMinutesData();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }

    @RequestMapping(value = "/kaiPanLaZiJinTask")
    @ResponseBody
    public String kaiPanLaZiJinTask() {
        List list = Lists.newArrayList();
        try {
            kaiPanLaZiJinTask.开盘啦资金净额();
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
        return JSONObject.toJSONString(list);
    }


    @RequestMapping(value = "/queryZuoTiStatus")
    @ResponseBody
    public void queryZuoTiStatus() {
        try {
            List<TenXunStock> tenXunStocks = HtmlTools.httpGet("sz000938");
            R result= zuoTiStatusFeignService.queryZuoTiStatus(tenXunStocks.get(0));
            log.info(">>>>>>>>>> queryZuoTiStatus result={}",JSONObject.toJSONString(result));
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
    }

    @RequestMapping(value = "/zhangTingjuJiaoService")
    @ResponseBody
    public void zhangTingjuJiaoService() {
        try {
            zhangTingjuJiaoService.dieTingData(null);
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
    }
    @RequestMapping(value = "/weiPanJianQianService")
    @ResponseBody
    public void weiPanJianQianService(String key) {
        try {
            weiPanJianQianService.execute(key);
        } catch (Exception e) {
            log.error("CalculationController.count exception={}", e);
        }
    }

    @RequestMapping(value = "/ebaySearchByTypeService")
    @ResponseBody
    public void ebaySearchByTypeService(EbayRequest ebayRequest) {
        try {
            ebayService.getGoodDetailFeedback(ebayRequest.getGoodsUrl(), ebayRequest.getTotalPage(),ebayRequest.getName());
        } catch (Exception e) {
            log.error("ebaySearchByTypeService.count exception={}", e);
        }
    }
    @RequestMapping(value = "/warnVipNewsService")
    @ResponseBody
    public void warnVipNewsService(EbayRequest ebayRequest) {
        try {
            warnVipNewsService.process();
        } catch (Exception e) {
            log.error("ebaySearchByTypeService.count exception={}", e);
        }
    }


    public static void main(String[] args) throws IOException {

    }

}
